package service

import (
	"encoding/json"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/repository"
	"gitlab.yoyiit.com/youyi/go-core/store"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"golang.org/x/net/context"
)

type CounterpartyPermissionService interface {
	ListCounterpartyPermission(ctx context.Context, req *api.ListCounterpartyPermissionRequest) (*api.ListCounterpartyPermissionResponse, error)
	GetCounterpartyPermission(ctx context.Context, req *api.CounterpartyPermissionData) (*api.CounterpartyPermissionData, error)
	AddCounterpartyPermission(ctx context.Context, req *api.CounterpartyPermissionData) error
	EditCounterpartyPermission(ctx context.Context, req *api.CounterpartyPermissionData) error
	DeleteCounterpartyPermission(ctx context.Context, id int64) (err error)
	BatchDeleteCounterpartyPermission(ctx context.Context, req *api.BatchDeleteCounterpartyPermissionRequest) (err error)
}

type somsService struct {
	counterpartyPermissionRepo repo.CounterpartyPermissionRepo
}

func (s *somsService) ListCounterpartyPermission(ctx context.Context, req *api.ListCounterpartyPermissionRequest) (*api.ListCounterpartyPermissionResponse, error) {
	dbDatas, count, err := s.counterpartyPermissionRepo.List(ctx, "", req.PageNum, req.PageSize, &repo.CounterpartyPermissionDBDataParam{
		CounterpartyPermissionDBData: repo.CounterpartyPermissionDBData{
			CounterpartyId:        req.Data.CounterpartyId,
			CounterpartyContactId: req.Data.CounterpartyContactId,
			UserId:                req.Data.UserId,
			Username:              req.Data.Username,
			Nickname:              req.Data.Nickname,
			PermissionType:        req.Data.PermissionType,
		},
	})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	var result []*api.CounterpartyPermissionData
	if dbDatas != nil {
		result = make([]*api.CounterpartyPermissionData, len(*dbDatas))
		for i, data := range *dbDatas {
			result[i] = &api.CounterpartyPermissionData{
				Id:                    data.Id,
				OrganizationId:        data.OrganizationId,
				CreatedAt:             util.FormatDateTime(data.CreatedAt),
				UpdatedAt:             util.FormatDateTime(data.UpdatedAt),
				CreatedUserId:         data.CreatedUserId,
				UpdatedUserId:         data.UpdatedUserId,
				UpdatedUserNickname:   data.UpdatedUserNickname,
				CreatedUserNickname:   data.CreatedUserNickname,
				CounterpartyId:        data.CounterpartyId,
				CounterpartyContactId: data.CounterpartyContactId,
				UserId:                data.UserId,
				Username:              data.Username,
				Nickname:              data.Nickname,
				PermissionType:        data.PermissionType,
			}
		}
	}
	return &api.ListCounterpartyPermissionResponse{
		Data:  result,
		Count: count,
	}, nil
}

func (s *somsService) GetCounterpartyPermission(ctx context.Context, req *api.CounterpartyPermissionData) (*api.CounterpartyPermissionData, error) {
	data, err := s.counterpartyPermissionRepo.Get(ctx, &repo.CounterpartyPermissionDBData{
		BaseDBData: repository.BaseDBData{
			BaseCommonDBData: repository.BaseCommonDBData{
				Id: req.Id,
			},
		},
	})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	resp := &api.CounterpartyPermissionData{
		Id:                    data.Id,
		OrganizationId:        data.OrganizationId,
		CreatedAt:             util.FormatDateTime(data.CreatedAt),
		UpdatedAt:             util.FormatDateTime(data.UpdatedAt),
		CreatedUserId:         data.CreatedUserId,
		UpdatedUserId:         data.UpdatedUserId,
		UpdatedUserNickname:   data.UpdatedUserNickname,
		CreatedUserNickname:   data.CreatedUserNickname,
		CounterpartyId:        data.CounterpartyId,
		CounterpartyContactId: data.CounterpartyContactId,
		UserId:                data.UserId,
		Username:              data.Username,
		Nickname:              data.Nickname,
		PermissionType:        data.PermissionType,
	}
	return resp, nil
}

func (s *somsService) AddCounterpartyPermission(ctx context.Context, data *api.CounterpartyPermissionData) error {
	repoData := &repo.CounterpartyPermissionDBData{
		CounterpartyId:        data.CounterpartyId,
		CounterpartyContactId: data.CounterpartyContactId,
		UserId:                data.UserId,
		Username:              data.Username,
		Nickname:              data.Nickname,
		PermissionType:        data.PermissionType,
	}
	_, err := s.counterpartyPermissionRepo.Add(ctx, repoData)
	if err != nil {
		return handler.HandleError(err)
	}
	return nil
}

func (s *somsService) EditCounterpartyPermission(ctx context.Context, data *api.CounterpartyPermissionData) error {
	repoData := &repo.CounterpartyPermissionDBData{
		BaseDBData: repository.BaseDBData{
			BaseCommonDBData: repository.BaseCommonDBData{
				Id: data.Id,
			},
		},
		CounterpartyId:        data.CounterpartyId,
		CounterpartyContactId: data.CounterpartyContactId,
		UserId:                data.UserId,
		Username:              data.Username,
		Nickname:              data.Nickname,
		PermissionType:        data.PermissionType,
	}
	return handler.HandleError(s.counterpartyPermissionRepo.UpdateById(ctx, data.Id, repoData))
}

func (s *somsService) DeleteCounterpartyPermission(ctx context.Context, id int64) (err error) {
	return handler.HandleError(s.counterpartyPermissionRepo.DeleteById(ctx, id))
}

func (s *somsService) BatchDeleteCounterpartyPermission(ctx context.Context, req *api.BatchDeleteCounterpartyPermissionRequest) (err error) {
	if req.Ids != nil && len(req.Ids) > 0 {
		// 自行补充逻辑
	}
	return nil
}
